Unity 路径问题

最近使用Handheld.PlayFullScreenMovie播放视频,因为不想放在StreamingAssets下导致包的大小太大。
所以,想着使用WWW下载存储在本地,记录存储路径,然后播放视频。测试后发现安卓是没有问题的,iOS路径不对。
一开始以为是设置权限问题,结果找了半天,还是没找到,然后把视频模块拆出来,单独测试,结果还是一样,最后发现是iOS路径前要加”file://“。
无法形容我的心情,WWW加载StreamingAssets下的配置文件时,也出现了路径问题,这个因为网上问的人比较多,所以很快找到了问题,也是路径前加”file:///“。
我还在想是不是iOS的Handheld.PlayFullScreenMovie不支持其他路径播放,我还是太单纯了。
下面是各个路径的分类:

  1. 加载StreamingAssets下的文件路径

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public string dirPath =
    #if UNITY_ANDROID && !UNITY_EDITOR
    Application.streamingAssetsPath;
    #elif UNITY_IPHONE && !UNITY_EDITOR
    "file:///"+ Application.streamingAssetsPath;
    #elif UNITY_STANDALONE_WIN || UNITY_EDITOR
    "file:///" + Application.dataPath + "/StreamingAssets";
    #else
    string.Empty;
    #endif
  2. 存储视频路径 和使用Handheld.PlayFullScreenMovie 播放存储视频路径

  • 一般视频下载完后(WWW下载),我都存储在Application.persistentDataPath 下;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public void CreateFile(byte[] bytes, string filePathName)
    {
    Stream str;
    if (!File.Exists(filePathName))
    {
    FileInfo file = new FileInfo(filePathName);
    str = file.Create();
    //文件写入
    str.Write(bytes, 0, bytes.Length);
    //关闭并销毁流
    str.Close();
    str.Dispose();
    }
    }
  • 写入完成后,播放视频的路径:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public string dirPath =
    #if UNITY_ANDROID && !UNITY_EDITOR
    Application.persistentDataPath;
    #elif UNITY_IPHONE && !UNITY_EDITOR
    "file://"+ Application.persistentDataPath;
    #elif UNITY_STANDALONE_WIN || UNITY_EDITOR
    Application.persistentDataPath;
    #else
    string.Empty;
    #endif

下次如果遇到路径问题,第一时间要想到是否加”file://“ 或”file:///“。

坚持原创技术分享,您的支持将鼓励我继续创作!